#!/usr/bin/perl -w
#################################
#Juca - 21/02/2012
#Dynect -> Quebec API
###############################

use IO::Handle;
use warnings;
use Sys::Hostname;
use POSIX;

use DB_File;

tie (%control, 'DB_File', "/export/scripts/dynect-quebec/control.dbm", O_CREAT|O_RDWR, 0644) || die $!;
$pipe = "/tmp/testes.log";
open (TO, ">> $pipe") or die("Erro ao tentar abrir ");

STDOUT->autoflush(1);
STDERR->autoflush(1);
STDIN->autoflush(1);

my $time;
my $syslogserver;
my $domain;
my $origin;
my $status;
my $resptime;
my $msg;
my @array;
my $host;
my $intervalo = 10;
my $signal_mask = POSIX::SigSet->new( SIGALRM );
my $signal_action = POSIX::SigAction->new( sub { &ev(); },$signal_mask,);
my $signal_oldaction = POSIX::SigAction->new();
sigaction( SIGALRM, $signal_action, $signal_oldaction );


#Testing
#my $log = UOL::Log::SysLog->new($syslog_facility,"logger");
#$log->escreve('info',"LogMonitor Started");

alarm($intervalo);


while (<STDIN>)
{
	chomp;
       if ( $_ = /(\w+\s+\d+\s\d+:\d+:\d+)\s(\d+.\d+.\d+.\d+)\s(\w+:)\s(\S+)\sprobe\s\d+:\s(\w+)\s(\w+)\s(\d+\.\d+)\s\"(.*)\"\s(\d+.\d+.\d+.\d+)/ ) {
                $time = $1;
                $syslogserver = $2;
                $domain = $4;
                $origin = $5;
                $status = $6;
                $resptime = $7;
                $mgs = $8;
                $host = $9;
		$i++;
		#print "vou gravar = $i ... $time $syslogserver $domain $origin $status $resptime $mgs $host" ;
		$array[$i]="$time $syslogserver $domain $origin $status $resptime $mgs $host\n";
		$key="$domain" . "_" . "$origin" . "_" . "$host";
		$control{$key} = $status;
        }
        undef $time;
        undef $msg;
        undef $syslogserver;
        undef $domain;
        undef $origin;
        undef $status;
        undef $resptime;
        undef $host;

}

sub ev {
        #$log->escreve('info',"sendlog-pass 60 $host AGENT_LASTRUN $mytime OK $ok PASS $pass013 PASSMON $pass413");
        print TO @array;
	close TO;
	untie %control;
	open (TO, ">> $pipe") or die("Erro ao tentar abrir ");
	tie (%control, 'DB_File', "/export/scripts/dynect-quebec/control.dbm", O_CREAT|O_RDWR, 0644) || die $!;
	undef @array;
	undef $time;
	undef $i;
	undef $syslogserver;
	undef $domain;
	undef $origin;
	undef $status;
	undef $resptime;
	undef $host;
        #$log->escreve('info',"sendlog-pass: timestamp $mytime OK $ok PASS $pass013 PASSMON $pass413");
        alarm($intervalo);
}

=cut
$pipe = "/tmp/testes.log";
open (TO, "> $pipe") or die("Erro ao tentar abrir ");

print (TO "valendno...");

while (<STDIN>) {
	print (TO "entrei no while...");
	#chomp;
#Feb 21 20:49:31 192.168.192.145 dynect: gslb.ipv6uol.com.br probe 1329857280: IAD down 10.000600 "timeout in chunked phase" 200.221.138.65
#Feb 21 20:50:31 192.168.192.145 dynect: gslb.ipv6uol.com.br probe 1329857340: MIA up 4.294900 "" 200.221.8.126
#Feb 21 20:50:31 192.168.192.145 dynect: gslb.ipv6uol.com.br probe 1329857340: EWR up 4.333700 "" 200.221.8.126
	
	#if ( $_ = /(\w+\s+\d+\s\d+:\d+:\d+)\s(\d+.\d+.\d+.\d+)\s(\w+:)\s(\w+)\sprobe\s\d+:\s(\w+)\s(\w+)\s(\d+)\s"(\w+)\s(\d+.\d+.\d+.\d+)/ ) {


	if ( $_ = /(\w+\s+\d+\s\d+:\d+:\d+)\s(\d+.\d+.\d+.\d+)\s(\w+:)\s(\S+)\sprobe\s\d+:\s(\w+)\s(\w+)\s(\d+\.\d+)\s\"(.*)\"\s(\d+.\d+.\d+.\d+)/ ) {
		print "entrei no if...";
		$time = $1;
		$syslogserver = $2;
		$domain = $4;
		$origin = $5;
		$status = $6;
		$resptime = $7;
		$mgs = $8;
		$host = $9;
	}
	#print LOG "$time $syslogserver $domain $origin $status $resptime $mgs $host\n"; 
	#print "$time $syslogserver $domain $origin $status $resptime $mgs $host\n"; 
        print (TO "xxx : $time $syslogserver $domain $origin $status $resptime $mgs $host\n");


}
#close(LOG);


        #print "Verificando: $user...";
        if (exists $mydbm{$user}) {
                #print "User ja existe\n";
                #print "$user";

        }
        else
        {
                #print "Adicionando\n";
                $mydbm{$user} = $now;
                print "$user";
        }

        #print "user: $user\n";

}



        while ( ($k,$v) = each %mydbm ) {
        #print "$k => $v\n";
        }

untie %mydbm;

